﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace JzSport.Api
{
    class GetActivityRequestJoinUsersAction : GetAction<GetActivityRequestJoinUsersParams,
        GetActivityRequestJoinUsersResult>
    {
        protected override ActionResult<GetActivityRequestJoinUsersResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetActivityRequestJoinUsersParams opParams)
        {
            int start = opParams.Page * 9999;
            int limit = 9999;
            ServiceResult<PagedList<Model.Data.ActivityMember>> serviceResult = new ActivityService().GetRequestJoinUsers(opParams.ActivityId, start, limit);

            GetActivityRequestJoinUsersResult actionResult = new GetActivityRequestJoinUsersResult();

            if (serviceResult.ErrorCode == ErrorCode.NoError)
            {
                actionResult.Page = opParams.Page;
                actionResult.Start = serviceResult.Data.Start;
                actionResult.Limit = serviceResult.Data.Limit;
                actionResult.Total = serviceResult.Data.Total;

                for (int index = 0; index < serviceResult.Data.Items.Count; index++)
                {
                    GetActivityRequestJoinUsersResult.MemberItem item = new GetActivityRequestJoinUsersResult.MemberItem();
                    item.Id = serviceResult.Data.Items[index].Id;
                    item.NickName = serviceResult.Data.Items[index].NickName;
                    item.AvatarUrl = string.Format("{0}{1}", Configs.BaseWebUrl, serviceResult.Data.Items[index].AvatarUrl); ;
                    item.Type = serviceResult.Data.Items[index].Type;
                    item.JoinTime = serviceResult.Data.Items[index].JoinTime;
                    item.Sex = serviceResult.Data.Items[index].Sex;
                    item.City = serviceResult.Data.Items[index].City;

                    actionResult.Items.Add(item);
                }
                return new ActionResult<GetActivityRequestJoinUsersResult>("1.0.0", true, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
            return new ActionResult<GetActivityRequestJoinUsersResult>("1.0.0", false, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
        }
    }

    public class GetActivityRequestJoinUsersParams : IActionParams
    {
        public Guid ActivityId { get; set; }
        public int Page { get; set; }

        public void ParseQueryString(HttpRequest request)
        {
            this.ActivityId = new Guid(request.QueryString["op:activityid"]);
            this.Page = Convert.ToInt32(request.QueryString["op:page"]);
        }

        public void ParseForm(HttpRequest request)
        {
            this.ActivityId = new Guid(request.Form["op:activityid"]);
            this.Page = Convert.ToInt32(request.Form["op:page"]);
        }
    }
    public class GetActivityRequestJoinUsersResult
    {
        public int Start { get; set; }
        public int Limit { get; set; }
        public int Total { get; set; }
        public int Page { get; set; }

        public bool IsTheEnd { get; set; }

        public List<MemberItem> Items { get; set; }

        public GetActivityRequestJoinUsersResult()
        {
            Items = new List<MemberItem>();
        }

        public class MemberItem
        {
            public int Id { get; set; }
            public string NickName { get; set; }
            public string AvatarUrl { get; set; }
            public int Type { get; set; }
            public DateTime JoinTime { get; set; }
            public int Sex { get; set; }
            public string City { get; set; }
        }
    }
}
